package com.uenpay.oem.ui.main.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.socks.library.KLog;
import com.uenpay.im.callback.IPopupWindowListener;
import com.uenpay.im.entities.PopupEntity;
import com.uenpay.im.ui.contacts.ContactsActivity;
import com.uenpay.im.ui.old.activity.contacts.SearchFriendsActivity;
import com.uenpay.im.ui.old.activity.group.ChooseCreateGroupTypeActivity;
import com.uenpay.im.view.MyPopupWindow;
import com.uenpay.im.zxing.CaptureActivity;
import com.uenpay.oem.R;
import com.uenpay.oem.adapter.HomeAdapter;
import com.uenpay.oem.adapter.HomeGridAdapter;
import com.uenpay.oem.data.bean.HomeGridEntity;
import com.uenpay.oem.ui.TestActivity;
import com.uenpay.oem.widget.behavior.HomeBarBehavior;
import com.uenpay.util.common.App;
import com.uenpay.util.common.DensityUtils;
import com.uenpay.util.widget.selector.AlphaSelector;
import com.uenpay.util.widget.selector.SelectorProcessor;
import com.uenpay.util.widget.wraprecyclerview.SimpleDividerItemDecoration;
import com.uenpay.util.widget.wraprecyclerview.WrapRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by DELL on 2017/4/27.
 */
public class HomeFragment extends Fragment {
    private static final String TAG = "HomeFragment";
    private View view;
    @BindView(R.id.recylerView)
    WrapRecyclerView recyclerView;
    @BindView(R.id.rl_home_titlebar_collapse)
    RelativeLayout titleBarCollapse;
    @BindView(R.id.ll_home_titlebar_expand)
    LinearLayout titleBarExpand;
    @BindView(R.id.appbarlayout)
    AppBarLayout appBarLayout;

    RelativeLayout flRecordLife;
    @BindView(R.id.ll_top_1)
    @AlphaSelector(R.id.ll_top_1)
    LinearLayout llTop1;
    @AlphaSelector(R.id.ll_top_2)
    @BindView(R.id.ll_top_2)
    LinearLayout llTop2;
    @AlphaSelector(R.id.ll_top_3)
    @BindView(R.id.ll_top_3)
    LinearLayout llTop3;
    @AlphaSelector(R.id.title_right_contacts_iv)
    @BindView(R.id.title_right_contacts_iv)
    ImageView ivContacts;
    @AlphaSelector(R.id.title_right_add_iv)
    @BindView(R.id.title_right_add_iv)
    ImageView ivAdd;
    @AlphaSelector(R.id.iv_titlebar_scan_code)
    @BindView(R.id.iv_titlebar_scan_code)
    ImageView ivScanCode;
    @AlphaSelector(R.id.iv_titlebar_pay)
    @BindView(R.id.iv_titlebar_pay)
    ImageView ivPay;
    @AlphaSelector(R.id.iv_titlebar_search)
    @BindView(R.id.iv_titlebar_search)
    ImageView ivSearch;
    @AlphaSelector(R.id.iv_titlebar_add)
    @BindView(R.id.iv_titlebar_add)
    ImageView ivMore;
    RecyclerView rvGrid;
    private HomeGridAdapter homeGridAdapter;
    private MyPopupWindow myPopupWindow;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_home_layout, container,
                    false);
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    private void initViews() {
        rvGrid = new RecyclerView(getActivity());
        rvGrid.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        rvGrid.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        rvGrid.setAdapter(new HomeGridAdapter(getActivity(), getHomeGridData()));

        flRecordLife = (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.include_record_my_life, null, false);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        TextView text = new TextView(getActivity());
        text.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        text.setText("this is a headerview ");
        List<View> list = new ArrayList<>();
        list.add(rvGrid);
        list.add(flRecordLife);
        recyclerView.addHeaderView(list);
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), DensityUtils.dip2px(8)));
        recyclerView.setAdapter(new HomeAdapter(getActivity()));


        myPopupWindow = new MyPopupWindow(getActivity(), getPopData(), new IPopupWindowListener() {

            @Override
            public void itemClick(int index) {
                if (index == 1) {
                    Intent intent = new Intent(getActivity(), ChooseCreateGroupTypeActivity.class);
                    startActivity(intent);
                } else if (index == 2) {
                    Intent intent = new Intent(getActivity(), SearchFriendsActivity.class);
                    startActivity(intent);
                }/* else if (index == 3) {
                    Intent intent = new Intent(getActivity(), SearchGroupsActivity.class);
                    startActivity(intent);
                }*/ else if (index == 3) {
                    Intent intent = new Intent(getActivity(), CaptureActivity.class);
                    startActivity(intent);
                } else if (index == 4) {
//                    Intent intent = new Intent(getActivity(), CreateCompanyActivity.class);
//                    startActivity(intent);
                }
            }
        });

        ((HomeBarBehavior) ((CoordinatorLayout.LayoutParams) recyclerView.getLayoutParams()).getBehavior()).setTitleBars(titleBarCollapse, titleBarExpand);
        SelectorProcessor selector = new SelectorProcessor();
        selector.process(this, view);
    }

    private ArrayList<PopupEntity> getPopData() {
        ArrayList<PopupEntity> arrayList = new ArrayList<PopupEntity>();
        PopupEntity entity = new PopupEntity(com.uenpay.im.R.drawable.icon_pop_chat, com.uenpay.im.R.string.start_group, 1);
        PopupEntity entity1 = new PopupEntity(com.uenpay.im.R.drawable.icon_pop_newfriend, com.uenpay.im.R.string.add_friends, 2);
        PopupEntity entity2 = new PopupEntity(com.uenpay.im.R.drawable.icon_pop_scan, com.uenpay.im.R.string.sweep, 3);
        PopupEntity entity3 = new PopupEntity(com.uenpay.im.R.drawable.icon_pop_money, com.uenpay.im.R.string.get_money, 4);
        arrayList.add(entity);
        arrayList.add(entity1);
        arrayList.add(entity2);
        arrayList.add(entity3);
        return arrayList;
    }

    @OnClick({R.id.ll_top_1, R.id.iv_titlebar_scan_code})
    public void top1Click(View v) {
        App.toast("click：扫一扫");
        Intent it = new Intent(getActivity(), TestActivity.class);
        startActivity(it);
    }

    @OnClick(R.id.ll_top_2)
    public void top2Click(View v) {
        App.toast("click:收钱");
    }

    @OnClick({R.id.ll_top_3, R.id.iv_titlebar_pay})
    public void top3Click(View v) {
        App.toast("click:付钱");
    }

    @OnClick(R.id.title_right_contacts_iv)
    public void goToContacts() {
        getActivity().startActivity(new Intent(getActivity(), ContactsActivity.class));
    }

    @OnClick({R.id.title_right_add_iv, R.id.iv_titlebar_add})
    public void popMore(View view) {
        if (myPopupWindow != null) {
            myPopupWindow.show(view);
        }
    }

    @OnClick(R.id.iv_titlebar_search)
    public void search() {
        App.toast("click search");
    }


    private List<HomeGridEntity> getHomeGridData() {
        List<HomeGridEntity> list = new ArrayList<>();
        list.add(new HomeGridEntity("转账", R.drawable.ic_home_grid_transfer));
        list.add(new HomeGridEntity("信用卡还款", R.drawable.ic_home_grid_credit));
        list.add(new HomeGridEntity("充值中心", R.drawable.ic_home_grid_recharge));
        list.add(new HomeGridEntity("余额宝", R.drawable.ic_home_grid_balance));
        list.add(new HomeGridEntity("商城", R.drawable.ic_home_grid_mall));
        list.add(new HomeGridEntity("生活缴费", R.drawable.ic_home_grid_life));
        list.add(new HomeGridEntity("火车票机票", R.drawable.ic_home_grid_airticket));
        list.add(new HomeGridEntity("全部", R.drawable.ic_home_grid_all));
        return list;
    }
}
